540C - Ice Cave - CodeForces Solution


dfs and similar *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define stream(line) istringstream in((line))
#define endl "\n"
#define F first
#define S second
#define clr(a, d) memset((a),d,sizeof(a))
const int MOD = 1e9 + 7;
const int N = 500 + 5;
const int M = 2e6 + 5;

void print_vector(vector<ll> &v) {
    for (ll i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    cout << endl;
}
int n,m;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
bool vis[N][N],f;
char arr[N][N];
int st,en,st2,en2,cnt;
void dfs(int i,int j){
    vis[i][j] = 1;
    if(arr[i][j] == 'X' && st2 == i && en2 == j){
        f = true;
        cnt++;
    }
    if(arr[i][j] == '.' && st2 == i && en2 == j){
        arr[i][j] = 'X';
        cnt++;
    }
    for (int k = 0; k < 4; ++k) {
        int x = i+dx[k], y = j+dy[k];
        if(x >=0 && y >= 0 && x <n && y < m && !vis[x][y] && arr[x][y] == '.'){
            dfs(x,y);
        }
        else if(x == st2 && y == en2){
            dfs(x,y);
        }
    }
}


int main() {
    fast
    int tc = 1;
    //cin >> tc;
    while (tc--) {
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                cin >> arr[i][j];
            }
        }
        cin >> st >> en >> st2 >> en2;
        st--,en--,st2--,en2--;
        f = false;
        dfs(st,en);
        if(st == st2 && en == en2 && cnt < 2){
            f = false;
        }
        if(f){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
    return 0;
}
			     		   		  	  	   	   			


Comments

Submit
0 Comments
More Questions

1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple